package com.iretailer.report.dto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @Auther Bennie
 * @Date 2020/6/3 13:57
 * @Description
 */
@Data
public class QueryOption {

    private int limit;
    private int offset;
    private String sort;
    private String order;

    private int current;

    public int getCurrent() {
        if (this.offset != 0 && limit != 0) {
            if (this.offset < this.limit) {
                return 1;
            } else {
                return offset % limit == 0 ? offset / limit + 1 : offset / limit;
            }
        } else {
            return 1;
        }
    }

    public void judge( QueryWrapper<?> queryWrapper) {
        if (StringUtils.isNotBlank(this.sort)) {
            if(StringUtils.isBlank(this.order)|| "asc".equalsIgnoreCase(this.order)){
                queryWrapper.orderByAsc(this.sort);
            }else{
                queryWrapper.orderByDesc(this.sort);
            }
        }
    }

    public void judge(Page<?> page, QueryWrapper<?> queryWrapper) {
        page.setCurrent(this.getCurrent());
        page.setSize(this.limit);
        if (StringUtils.isNotBlank(this.sort)) {
            if(StringUtils.isBlank(this.order)|| this.order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(this.sort);
            }else{
                queryWrapper.orderByDesc(this.sort);
            }
        }
    }


}
